<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['article:funprompts:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['article:funprompts:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['article:funprompts:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['article:funprompts:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="funpromptsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="使用模型" align="center" prop="useModel" />
      <el-table-column label="随机性" align="center" prop="temperature" />
      <el-table-column label="核采样" align="center" prop="topP" />
      <el-table-column label="单次回复限制" align="center" prop="maxTokens" />
      <el-table-column label="话题新鲜度" align="center" prop="presencePenalty" />
      <el-table-column label="频率惩罚度" align="center" prop="frequencyPenalty" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['article:funprompts:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['article:funprompts:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改功能参数对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>

        <el-form-item label="生成要求" prop="systemRole">
          <el-input v-model="form.systemRole" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-form-item label="使用模型" prop="useModel">
          <el-select
            v-model="useModel"
            ref="optionPromtsRef"
            placeholder="使用模型"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.gpt_model"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="随机性" prop="temperature">
          <div class="block">
            <span class="demonstration">值越大，回复越随机</span>
            <el-slider v-model="form.temperature"   :max="1"   :min="0" :step="0.1" :format-tooltip="formatTooltip"></el-slider>
          </div>
<!--          <el-input v-model="form.temperature" placeholder="请输入随机性" />-->
        </el-form-item>

        <el-form-item label="核采样" prop="topP">
<!--          <el-input v-model="form.topP" placeholder="请输入核采样" />-->
          <div class="block">
          <span class="demonstration">与随机性类似，但不要和随机性一起更改</span>
          <el-slider v-model="form.topP" :max="1"   :min="0" :step="0.1" :format-tooltip="formatTooltip"></el-slider>
        </div>
        </el-form-item>
        <el-form-item label="单次回复限制" prop="maxTokens">
<!--          <el-input v-model="form.maxTokens" placeholder="请输入单次回复限制" />-->
          <el-input-number size="mini" :min="1500" :max="16000" :step="200" v-model="form.maxTokens"></el-input-number>
        </el-form-item>
        <el-form-item label="话题新鲜度" prop="presencePenalty">
          <span class="demonstration">值越大，越有可能扩展到新话题</span>
          <el-slider v-model="form.presencePenalty"  :max="1"   :min="0" :step="0.1" :format-tooltip="formatTooltip"></el-slider>
<!--          <el-input v-model="form.presencePenalty" placeholder="请输入话题新鲜度" />-->
        </el-form-item>
        <el-form-item label="频率惩罚度" prop="frequencyPenalty">
          <span class="demonstration">值越大，越有可能降低重复字词</span>
          <el-slider v-model="form.frequencyPenalty" :max="1"   :min="0" :step="0.1" :format-tooltip="formatTooltip"></el-slider>
<!--          <el-input v-model="form.frequencyPenalty" placeholder="请输入频率惩罚度" />-->
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFunprompts, getFunprompts, delFunprompts, addFunprompts, updateFunprompts } from "@/api/article/funprompts";

export default {
  name: "Funprompts",
  dicts: ['gpt_model'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 功能参数表格数据
      funpromptsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      useModel:'gpt-3.5-turbo-16k',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        systemRole: [
          { required: true, message: "扮演角色不能为空", trigger: "change" }
        ],
        useModel: [
          { required: true, message: "使用模型不能为空", trigger: "change" }
        ],
        temperature: [
          { required: true, message: "随机性不能为空", trigger: "change" }
        ],
        topP: [
          { required: true, message: "核采样不能为空", trigger: "change" }
        ],
        maxTokens: [
          { required: true, message: "单次回复限制不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatTooltip(val) {
      console.log(val)
      return val;
    },
    /** 查询功能参数列表 */
    getList() {
      this.loading = true;
      listFunprompts(this.queryParams).then(response => {
        this.funpromptsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        useModel: 'gpt-3.5-turbo-16k',
        temperature: 0,
        systemRole:null,
        topP: 0,
        maxTokens: 4000,
        presencePenalty: 0,
        frequencyPenalty: 0,
        createTime: null,
        status: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加功能参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFunprompts(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改功能参数";
      });
    },
    /** 提交按钮 */
    submitForm() {


      var model_id =  this.$refs.optionPromtsRef.selected.value;
      var model_name =  this.$refs.optionPromtsRef.selected.label;
      this.form.useModel = model_name;

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFunprompts(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFunprompts(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除功能参数编号为"' + ids + '"的数据项？').then(function() {
        return delFunprompts(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('article/funprompts/export', {
        ...this.queryParams
      }, `funprompts_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
